#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
//#define ll long long
const int maxn = 2e5+10;
struct E{
    int b, w;
};
int N;
vector<E> G[maxn];
ll F[maxn];

void dfs(int u, int f){
    for(E e:G[u]){
        ll v = e.b, w = e.w;
        if(v == f) continue;
        dfs(v, u);
        if(G[v].size()==1) F[u] += w;
        else F[u] += min(F[v], w);
    }
}

void dp(int u, int f, ll w){
    if(u != f){
        if(G[f].size()==1) F[u] += w;
        else{
            ll t;
            if(G[u].size()==1) t = F[f] - w;
            else t = F[f] - min(w, F[u]);
            F[u] += min(t, w);
        }

    }
    for(E e:G[u]){
        if(e.b == f) continue;
        dp(e.b, u, e.w);
    }
}


signed main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);
    int T; cin >> T;
    while (T--){
        cin >> N;
        for (int i = 0; i < N - 1; ++i) {
            int a, b, w; cin >> a >> b >> w;
            G[a].push_back({b, w});
            G[b].push_back({a, w});
        }

        dfs(1, 1);
        dp(1, 1, 0);
        ll ans = 0;
        for (int i = 1; i <= N ; ++i) {
            ans = max(ans, F[i]);
        }
        cout << ans << "\n";
        memset(F, 0, sizeof F);
        for (int i = 1; i <=N ; ++i) {
            G[i].clear();
        }
    }
    return 0;
}